package com.situ.fly.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.fly.entity.Sign;
import com.situ.fly.entity.SignStatus;
import com.situ.fly.entity.User;
import com.situ.fly.mapper.SignMapper;
import com.situ.fly.mapper.UserMapper;
import com.situ.fly.service.SignService;

@Service
public class SignServiceImpl implements SignService {

	@Autowired
	private SignMapper signMapper;
	@Autowired
	private UserMapper userMapper;

	@Override
	public SignStatus sign(User user) {
		SignStatus signStatus = new SignStatus();

		// 未登录
		if (user == null) {
			signStatus.setLogined(false);
			signStatus.setExperience(5);
			signStatus.setDays(0);

			return signStatus;
		}

		Sign sign = signMapper.selectByUid(user.getId());
		if (sign == null) {
			// 没有签到信息
			signMapper.insert(user.getId());

			signStatus.setSigned(true);
			signStatus.setExperience(5);
			signStatus.setDays(1);

		} else {
			// 已有签到信息
			// 判断签到时间与当前时间差
			long day = checkDay(sign.getLasttime());

			if (day == 0) {
				signStatus.setSigned(true);
				signStatus.setExperience(checkExperience(sign.getCount()));
				signStatus.setDays(sign.getCount());
			} else{
				signStatus.setLogined(true);
				
				if (day == 1) {
					sign.setCount(sign.getCount() + 1);
				} else {
					sign.setCount(1);
				}

				signMapper.update(sign);
				user.setKiss(user.getKiss() + checkExperience(sign.getCount()));
				userMapper.update(user);

				signStatus.setSigned(true);
				signStatus.setExperience(checkExperience(sign.getCount()));
				signStatus.setDays(sign.getCount());
			} 
		}

		return signStatus;
	}

	private Integer checkExperience(Integer count) {
		if (count < 5) {
			return 5;
		} else if (count < 15) {
			return 10;
		} else if (count < 30) {
			return 15;
		} else {
			return 20;
		}
	}

	private long checkDay(Date lasttime) {
		// 清零
		lasttime.setHours(0);
		lasttime.setMinutes(0);
		lasttime.setSeconds(0);

		Date now = new Date();
		now.setHours(0);
		now.setMinutes(0);
		now.setSeconds(0);

		// 时间差的秒数
		long time = (now.getTime() - lasttime.getTime()) / 1000;

		return time / (24 * 60 * 60);
	}

	@Override
	public SignStatus getSignStatus(User user) {
		SignStatus signStatus = new SignStatus();

		if (user == null) {
			signStatus.setLogined(false);
			signStatus.setExperience(5);
			signStatus.setDays(0);

			return signStatus;
		}

		signStatus.setLogined(true);

		Sign sign = signMapper.selectByUid(user.getId());

		if (sign == null) {
			signStatus.setDays(0);
			signStatus.setExperience(5);
			signStatus.setSigned(false);
		} else {
			signStatus.setLogined(true);
			// 判断签到时间与当前时间差
			long day = checkDay(sign.getLasttime());

			if (day == 0) {
				signStatus.setSigned(true);
				signStatus.setExperience(checkExperience(sign.getCount()));
				signStatus.setDays(sign.getCount());
			} else {
				if (day == 1) {
					sign.setCount(sign.getCount() + 1);
				} else {
					sign.setCount(1);
				}

				signStatus.setSigned(false);
				signStatus.setExperience(checkExperience(sign.getCount()));
				signStatus.setDays(sign.getCount()-1);
			}

		}

		return signStatus;
	}

	@Override
	public List getNewRanking() {
		return signMapper.selectOrderByTimeDesc();
	}

	@Override
	public List getFastRanking() {
		return signMapper.selectOrderByTime();
	}

	@Override
	public List getTotalRanking() {
		return signMapper.selectOrderByCountDesc();
	}

}
